#include "ftmultibutton.h"

FTMultiButton::FTMultiButton(FTControls::IconSize is, FTMultiButton::multiMode mm) :
    FTBasicWidget()
{
    base_size = FTControls::iconSize( is );    
    totalWidth = 0;
    selected = -99999;
    useClock = false;
    clock_timer = -1;
    deviceInfo = NULL;
    setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
    setOrientation( FTControls::horizontal );
    images.clear();
    titles.clear();
    ids.clear();
    if ( mm == FTMultiButton::auto_select )
        connect( this, SIGNAL(clicked(int)),
                 this, SLOT(selectButton(int)) );
}

void FTMultiButton::enableClock(bool c)
{
    useClock = c;
    if ( useClock ) {
        clock_timer = startTimer( 60000 );
        deviceInfo = new QSystemDeviceInfo(this);
    }
    else {
        killTimer( clock_timer );
        delete deviceInfo;
    }
    update();
}

bool FTMultiButton::clockEnabled() const
{
    return useClock;
}

void FTMultiButton::timerEvent(QTimerEvent *)
{
    update();
}

QSize FTMultiButton::sizeHint() const
{
    int border = FTControls::defaultPadding();
    QFontMetrics fm(font());
    int myHeight = fm.size( Qt::TextSingleLine, "test" ).height()
                 + base_size.height()
                 + border*2
                 + border*2;
    int myWidth =  border*2 + totalWidth;

    if ( _orientation == FTControls::vertical )
        return QSize( myHeight, myWidth );
    else
        return QSize( myWidth, myHeight );
}

QSize FTMultiButton::minimumSizeHint() const
{
    return sizeHint();
}

void FTMultiButton::addButtonLeft( int k, const QImage& i, const QString& t )
{
    if ( !ids.contains(k) )
    {
        if ( selected == -99999 )
            selected = k;
        images.push_front( i );
        titles.push_front( t );
        ids.push_front( k );
        totalWidth += base_size.width();
    }
}

void FTMultiButton::addButtonRight( int key, const QImage& i, const QString& t )
{
    if ( !ids.contains(key) )
    {
        if ( selected == -99999 )
            selected = key;
        images.push_back( i );
        titles.push_back( t );
        ids.push_back( key );
        totalWidth += base_size.width();
    }
}

int FTMultiButton::selectedIndex() const
{
    return selected;
}

void FTMultiButton::selectButton(int key)
{
    if ( ids.contains(key) )
    {
        selected = key;
        update();
    }
}

void FTMultiButton::setImage(int key, const QImage & i)
{
    if ( ids.contains(key) )
    {
        images.replace(ids.indexOf(key), i);
        update();
    }
}

void FTMultiButton::setText(int key, const QString & t)
{
    if ( ids.contains(key) )
    {
        titles.replace(ids.indexOf(key), t);
        update();
    }
}

void FTMultiButton::paintEvent(QPaintEvent *)
{
    int border = FTControls::defaultPadding();
    QPalette palette = QApplication::palette();
    QPainter painter(this);

    int myHeight = height();
    int myWidth = width();
    // Shorthand for the button overall rect:
    QRect areaRect = rect();

    int offset_x = base_size.width();
    int offset_y = 0;
    // Rotate the painter and all the relevant values
    if ( _orientation == FTControls::vertical )
    {
        painter.rotate( -90 );
        painter.translate( -height(), 0);
        areaRect.setWidth( myHeight );
        areaRect.setHeight( myWidth );
        qSwap( myHeight, myWidth );
    }

    // Set default color group
    palette.setCurrentColorGroup( FT_COLOR_GROUP );

    QBrush overallBrush = palette.brush( QPalette::Button );

    QBrush selectedBrush = palette.color( QPalette::Highlight );
    QPen selectedPen = palette.color( QPalette::HighlightedText );

    QPen borderPen = palette.color( QPalette::Light );

    // Fill up the background...
    painter.fillRect( areaRect, overallBrush );

    // Draw the buttons...
    int left_span = (myWidth - totalWidth) / 2;
    QRect buttonRect = QRect( QPoint(border + left_span, border), base_size);

    int nButtons = ids.count();
    for (int b = 0; b < nButtons; b++ )
    {   // From left to right...
        int id = ids.at(b);
        if (id == selected)
        {
            painter.fillRect( buttonRect.adjusted(0,0,0, border*2), selectedBrush );
            painter.setPen( borderPen );
            painter.drawRect( buttonRect );
            QRect textRect = areaRect.adjusted( border,
                                                border + base_size.height(),
                                                -border,
                                                -border);
            // Draw the text
            painter.setPen( selectedPen );
            painter.fillRect( textRect, selectedBrush );
            QString txt = titles.at(b);
            if ( useClock ) {
                txt += " ("+QTime::currentTime().toString("hh:mm");
                txt += " bat:" + QString::number(deviceInfo->batteryLevel()) + "%)";
            }
            painter.drawText( textRect,Qt::AlignCenter, txt );
        }
        painter.drawImage( buttonRect, images.at(b));
        buttonRect.translate( offset_x, offset_y );
    }
}

void FTMultiButton::mouseReleaseEvent(QMouseEvent * me)
{
    int position;
    if ( _orientation == FTControls::vertical )
        position = (height()-me->pos().y()) - (height() - totalWidth) / 2;
    else
        position = me->pos().x() - (width() - totalWidth) / 2;

    position /= base_size.width();

    if ( (position > -1) && (position < ids.count() ) )
    {
        int id = ids.at(position);
        emit clicked( id );
    }
}

